package com.frank.oj.service.admin.system.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frank.model.entity.common.Announcement;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.model.vo.AnnouncementVO;
import com.frank.oj.service.admin.system.AnnouncementService;
import com.frank.oj.service.entity.common.AnnouncementEntityService;
import com.frank.oj.shiro.AccountProfile;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author frank
 * @Date 2024/3/28
 */
@Service
@Slf4j
public class AnnouncementServiceImpl implements AnnouncementService {
    @Resource
    private AnnouncementEntityService announcementEntityService;
    @Override
    public IPage<AnnouncementVO> getAnnouncementList(Integer limit, Integer currentPage) {

        if (currentPage==null||currentPage<1) currentPage=1;
        if (limit==null||limit<1) limit=10;

        return announcementEntityService.getAnnouncementList(limit,currentPage,false);
    }

    @Override
    public void addAnnouncement(Announcement announcement) throws StatusFailException {
        boolean save = announcementEntityService.save(announcement);
        if (!save){
            throw new StatusFailException("新增公告失败🙁");
        }
    }

    @Override
    public void deleteAnnouncement(Long aid) throws StatusFailException {
        boolean isOk = announcementEntityService.removeById(aid);
        if (!isOk) {
            throw new StatusFailException("删除失败🙁");
        }
        AccountProfile root = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        log.info("HC:[{}]删除了一个通知[{}]",root.getUsername(),aid);
    }

    @Override
    public void updateAnnouncement(Announcement announcement) throws StatusFailException {
        boolean update = announcementEntityService.saveOrUpdate(announcement);
        if (!update){
            throw new StatusFailException("修改失败🙁");
        }
    }
}
